﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using YWM;
using YWM.Localization;

namespace PLCWpfTest.Controls
{
    /// <summary>
    /// StationOperation.xaml 的交互逻辑
    /// </summary>
    public partial class StationOperation : UserControl
    {
        private static readonly DependencyProperty IdentifierProperty = DependencyProperty.Register(nameof(Identifier), typeof(byte), typeof(StationOperation), new PropertyMetadata((byte)1, null, IdentifierPropertyValueCallback));

        private static readonly DependencyProperty TempHighLimitProperty = DependencyProperty.Register(nameof(TempHighLimit), typeof(double), typeof(StationOperation), new PropertyMetadata(0.0, TempHighLimitPropertyChangedCallback, null));

        private static readonly DependencyProperty TempLowLimitProperty = DependencyProperty.Register(nameof(TempLowLimit), typeof(double), typeof(StationOperation), new PropertyMetadata(0.0, TempLowLimitPropertyChangedCallback, null));

        private static readonly DependencyProperty HumidityHighLimitProperty = DependencyProperty.Register(nameof(HumidityHighLimit), typeof(double), typeof(StationOperation), new PropertyMetadata(0.0, HumidityHighLimitPropertyChangedCallback, null));

        private static readonly DependencyProperty HumidityLowLimitProperty = DependencyProperty.Register(nameof(HumidityLowLimit), typeof(double), typeof(StationOperation), new PropertyMetadata(0.0, HumidityLowLimitPropertyChangedCallback, null));

        private static readonly DependencyProperty TempAlarmEnableProperty = DependencyProperty.Register(nameof(TempAlarmEnable), typeof(short), typeof(StationOperation), new PropertyMetadata((short)0, TempAlarmEnablePropertyChangedCallback, null));

        private static readonly DependencyProperty HumidityAlarmEnableProperty = DependencyProperty.Register(nameof(HumidityAlarmEnable), typeof(short), typeof(StationOperation), new PropertyMetadata((short)0, HumidityAlarmEnablePropertyChangedCallback, null));

        public byte Identifier
        {
            get { return (byte)GetValue(IdentifierProperty); }
            set { SetValue(IdentifierProperty, value); }
        }

        [Bindable(true), Category("自定义属性"), Description("温度高限值")]
        public double TempHighLimit
        {
            get { return (double)GetValue(TempHighLimitProperty); }
            set { SetValue(TempHighLimitProperty, value); }
        }

        [Bindable(true), Category("自定义属性"), Description("温度低限值")]
        public double TempLowLimit
        {
            get { return (double)GetValue(TempLowLimitProperty); }
            set { SetValue(TempLowLimitProperty, value); }
        }

        [Bindable(true), Category("自定义属性"), Description("湿度高限值")]
        public double HumidityHighLimit
        {
            get { return (double)GetValue(HumidityHighLimitProperty); }
            set { SetValue(HumidityHighLimitProperty, value); }
        }

        [Bindable(true), Category("自定义属性"), Description("湿度低限值")]
        public double HumidityLowLimit
        {
            get { return (double)GetValue(HumidityLowLimitProperty); }
            set { SetValue(HumidityLowLimitProperty, value); }
        }

        [Bindable(true), Category("自定义属性"), Description("温度启用报警")]
        public short TempAlarmEnable
        {
            get { return (short)GetValue(TempAlarmEnableProperty); }
            set { SetValue(TempAlarmEnableProperty, value); }
        }

        [Bindable(true), Category("自定义属性"), Description("湿度启用报警")]
        public short HumidityAlarmEnable
        {
            get { return (short)GetValue(HumidityAlarmEnableProperty); }
            set { SetValue(HumidityAlarmEnableProperty, value); }
        }

        public StationOperation()
        {
            InitializeComponent();
            TempAlarmEnableName.Checked += TempAlarmEnableName_Checked;
            TempAlarmEnableName.Unchecked += TempAlarmEnableName_Unchecked;
            HumidityAlarmEnableName.Checked += HumidityAlarmEnableName_Checked;
            HumidityAlarmEnableName.Unchecked += HumidityAlarmEnableName_Unchecked;
        }

        private static object IdentifierPropertyValueCallback(DependencyObject d, object baseValue)
        {
            if (baseValue == null)
            {
                return baseValue;
            }

            byte identifier = (byte)baseValue;
            StationOperation stationOperation = (StationOperation)d;
            ILocalizationManager lang = YApp.Resolve<ILocalizationManager>();
            if (lang != null)
            {
                string title = string.Format(lang["站点"], identifier);
                stationOperation.StationTitle.Title = title;
                stationOperation.TempHighLimitName.Text = $"{title}{lang["温度高限"]}";
                stationOperation.TempLowLimitName.Text = $"{title}{lang["温度低限"]}";
                stationOperation.HumidityHighLimitName.Text = $"{title}{lang["湿度高限"]}";
                stationOperation.HumidityLowLimitName.Text = $"{title}{lang["湿度低限"]}";
            }
            return identifier;
        }

        private static void TempHighLimitPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }

            double tempHighLimit = (double)e.NewValue;
            StationOperation stationOperation = (StationOperation)d;
            stationOperation.TempHighLimitValue.ValueChanged -= stationOperation.TempHighLimitValue_ValueChanged;
            stationOperation.TempHighLimitValue.Value = tempHighLimit;
            stationOperation.TempHighLimitValue.ValueChanged += stationOperation.TempHighLimitValue_ValueChanged;
        }

        private static void TempLowLimitPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }

            double tempLowLimit = (double)e.NewValue;
            StationOperation stationOperation = (StationOperation)d;
            stationOperation.TempLowLimitValue.ValueChanged -= stationOperation.TempLowLimitValue_ValueChanged;
            stationOperation.TempLowLimitValue.Value = tempLowLimit;
            stationOperation.TempLowLimitValue.ValueChanged += stationOperation.TempLowLimitValue_ValueChanged;
        }

        private static void HumidityHighLimitPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }

            double humidityHighLimit = (double)e.NewValue;
            StationOperation stationOperation = (StationOperation)d;
            stationOperation.HumidityHighLimitValue.ValueChanged -= stationOperation.HumidityHighLimitValue_ValueChanged;
            stationOperation.HumidityHighLimitValue.Value = humidityHighLimit;
            stationOperation.HumidityHighLimitValue.ValueChanged += stationOperation.HumidityHighLimitValue_ValueChanged;
        }

        private static void HumidityLowLimitPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }

            double humidityLowLimit = (double)e.NewValue;
            StationOperation stationOperation = (StationOperation)d;
            stationOperation.HumidityLowLimitValue.ValueChanged -= stationOperation.HumidityLowLimitValue_ValueChanged;
            stationOperation.HumidityLowLimitValue.Value = humidityLowLimit;
            stationOperation.HumidityLowLimitValue.ValueChanged += stationOperation.HumidityLowLimitValue_ValueChanged;
        }

        private static void TempAlarmEnablePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }

            short tempAlarmEnable = (short)e.NewValue;
            StationOperation stationOperation = (StationOperation)d;
            stationOperation.TempAlarmEnableName.Checked -= stationOperation.TempAlarmEnableName_Checked;
            stationOperation.TempAlarmEnableName.Unchecked -= stationOperation.TempAlarmEnableName_Unchecked;
            stationOperation.TempAlarmEnableName.IsChecked = tempAlarmEnable == 1;
            stationOperation.TempAlarmEnableName.Checked += stationOperation.TempAlarmEnableName_Checked;
            stationOperation.TempAlarmEnableName.Unchecked += stationOperation.TempAlarmEnableName_Unchecked;
        }

        private static void HumidityAlarmEnablePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }

            short humidityAlarmEnable = (short)e.NewValue;
            StationOperation stationOperation = (StationOperation)d;
            stationOperation.HumidityAlarmEnableName.Checked -= stationOperation.HumidityAlarmEnableName_Checked;
            stationOperation.HumidityAlarmEnableName.Unchecked -= stationOperation.HumidityAlarmEnableName_Unchecked;
            stationOperation.HumidityAlarmEnableName.IsChecked = humidityAlarmEnable == 1;
            stationOperation.HumidityAlarmEnableName.Checked += stationOperation.HumidityAlarmEnableName_Checked;
            stationOperation.HumidityAlarmEnableName.Unchecked += stationOperation.HumidityAlarmEnableName_Unchecked;
        }

        private void TempHighLimitValue_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double?> e)
        {
            TempHighLimit = TempHighLimitValue.Value.Value;
        }

        private void TempLowLimitValue_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double?> e)
        {
            TempLowLimit = TempLowLimitValue.Value.Value;
        }

        private void HumidityHighLimitValue_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double?> e)
        {
            HumidityHighLimit = HumidityHighLimitValue.Value.Value;
        }

        private void HumidityLowLimitValue_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double?> e)
        {
            HumidityLowLimit = HumidityLowLimitValue.Value.Value;
        }

        private void TempAlarmEnableName_Checked(object sender, RoutedEventArgs e)
        {
            TempAlarmEnable = 1;
        }

        private void TempAlarmEnableName_Unchecked(object sender, RoutedEventArgs e)
        {
            TempAlarmEnable = 0;
        }

        private void HumidityAlarmEnableName_Checked(object sender, RoutedEventArgs e)
        {
            HumidityAlarmEnable = 1;
        }

        private void HumidityAlarmEnableName_Unchecked(object sender, RoutedEventArgs e)
        {
            HumidityAlarmEnable = 0;
        }
    }
}
